class StatisticController < ApplicationController
  layout "company"
  def index
  end
  def activity
   @projects=@cur_user.company.projects
   @s_projects={}
   render :update do |page|
    page.replace_html 'stat_layout',:partial=>'activity'
    page.hide 'indicator'
   end
  end
  def activity_xml
  render :layout=>false;
  end
  def project
   @projects=@cur_user.company.projects
   @s_projects={}
   render :update do |page|
    page.replace_html 'stat_layout',:partial=>'project'
    page.hide 'indicator'
   end
  end

  def user
   @users=@cur_user.company.users
   @s_users={}
   render :update do |page|
    page.replace_html 'stat_layout',:partial=>'user'
    page.hide 'indicator'
   end
  end

  def result_activity
     @s_projects = params[:s_projects]
      if @s_projects
       @pids = @s_projects.join("|") 
      end
      render :update do |page|
      page.replace_html 'stat_layout',:partial=>'activity_view'
      end
  end

  #generate chart xml
  def activity_xml
    #@headers["Content-Type"] = "application/xml;charset=utf-8" 
    response.headers["Content-Type"] = "application/xml" 
    @pids = params[:pids]
    @pids = @pids.split("|").join(",")
    @projects= Project.find(:all,:conditions=>["id IN(#{@pids})"])
    respond_to do |format|
      format.xml {
       render :layout => false
      }
    end
  end

  def result_project
     @s_projects = params[:s_projects]
	 if @s_projects
	    p_array = @s_projects.join(",") 
	 end
	 @projects= Project.find(:all,:conditions=>["id IN(#{p_array.to_s})"])

	 start_at = params[:time_log][:start_at]
	 @confirmed_time = params[:time_log][:confirmed_time]
	 @work_time      = params[:time_log][:work_time]

	 @start_at = params[:time_log][:start_at].to_time.strftime(LONGDATE)
	 @end_at   = params[:time_log][:end_at].to_time.strftime(LONGDATE)

     render :update do |page|
	 page.replace_html 'result',:partial=>'project_view'
	 end
  end
  def result_user
     @s_users= params[:s_users]
	 if @s_users
	    p_array = @s_users.collect {|u| u + ","}
	    p_array.last.sub!(',','') 	
	 end

	 @users= User.find(:all,:conditions=>["id IN(#{p_array.to_s})"])

	 @start_at = params[:time_log][:start_at].to_time.strftime(LONGDATE)
	 @end_at   = params[:time_log][:end_at].to_time.strftime(LONGDATE)
	 @confirmed_time = params[:time_log][:confirmed_time]
	 @work_time      = params[:time_log][:work_time]
	 @include_completed_projects = params[:time_log][:include_completed_projects]


     render :update do |page|
	 page.replace_html 'result',:partial=>'user_view'
     end
  end
end
